angular.module('homeModule', ['ionic'])
	.controller('DashCtrl', function($scope, home, $ionicScrollDelegate) {
			home.getHomeData()
			.then(function(data) {
				$scope.banner = data.data.data.banner;
				$scope.season = data.data.data.inSeason;
				$scope.suitTabs = data.data.data.suitTabs;
				$scope.buyBlockList = data.data.data.buyBlockList;
				$scope.modules = data.data.data.modules;
			});
		home.getChoicenessList()
			.then(function(data) {
				$scope.choicelist = data.data.data.result;

			});
		$scope.page = 2;
		// this.$scope.page
		$scope.isLoadingMore = true;
		$scope.reqMore = function() {
			$scope.isLoadingMore = false;
			//console.log("请求更多了");
			home.getChoiceMore($scope.page)
				.then(function(data) {
					//console.log($scope.page);
					//console.log('刷新完成了');
					$scope.page++;
					$scope.choicelist = data.data.data.result;				
					$scope.$broadcast('scroll.infiniteScrollComplete');
					$scope.isLoadingMore = true;
				})
		};
		
		$scope.loadMore=function(){
			if($scope.isLoadingMore){
				$scope.reqMore();
			}
		}

	})
	.filter("myfilter",function(){
		return function(v){
			return v.split("=").splice(-1,1).join()
		}
	})
	.filter("myfil",function(){
		return function(v){
			return v.split("=").splice(1,1).join().split("&").splice(0,1).join()
		}
	})
	.factory('home', function($http) {
		var getHomeData = function() {
			//--https://ichuanyi.com/m.php?method=index.getHomeData
			return $http.get("/icy/m.php?method=index.getHomeData")
				.then(function(data) {
					return data;
				});
		};
		var getChoicenessList = function() {
			return $http.get('/icy/m.php?fromPageId=0&pageSize=20&viewUserId=&method=index.getChoicenessList', {
				params: {
					page: 1,
				}
			}).then(function(data) {
				return data;
			})
		};
		var getChoiceMore = function(i) {

			return $http.get('/icy/m.php?fromPageId=0&viewUserId=&method=index.getChoicenessList', {
				params: {
					pageSize:20*i
				}
			}).then(function(data) {
				return data;
			})
		};

		// https://ichuanyi.com/m.php?fromPageId=0&pageSize=20&viewUserId=&method=index.getChoicenessList&page=1
		return {
			getHomeData,
			getChoicenessList,
			getChoiceMore
		};
	})